var ChatWS = {};
ChatWS.state=0;

ChatWS.check=function(){
    if ('WebSocket' in window) {
        if (window.console) window.console.log('Info: support WebSocket.');
        return true;
    } else if ('MozWebSocket' in window) {
        if (window.console) window.console.log('Info: support MozWebSocket.');
        return true;
    } else {
        if (window.console) window.console.log('Error: WebSocket is not supported by this browser.');
        return false;
    }
}

ChatWS.close=function(){
    ChatWS.socket.close();
}

ChatWS.connect = function (host) {
    if ('WebSocket' in window) {
        if (window.console) window.console.log('Info: init Port WebSocket.');
        ChatWS.socket = new WebSocket(host);
    } else if ('MozWebSocket' in window) {
        if (window.console) window.console.log('Info: init Port MozWebSocket.');
        ChatWS.socket = new MozWebSocket(host);
    } else {
        if (window.console) window.console.log('Error: WebSocket is not supported by this browser.');
        return;
    }

    ChatWS.socket.onopen = function (event) {
        if (window.console) window.console.log('Info: Port WebSocket connection opened.' + event);
        ChatWS.state=1;
        ChatWS.onopen(event);
    };

    ChatWS.socket.onclose = function (event) {
        if (window.console) window.console.log('Info: Port WebSocket closed.' + event);
        try{
            startoffPolling();
        }catch(e) {
            if(window.console) console.log("startoffPolling 方法不存在");
        }

        /**
         * 设置为未连接
         * @type {number}
         */
        ChatWS.state=0;

        /**
         * 开始自动重连
         */
        /*if(!PortWS.reconnect) {
         PortWS.reconnect = setInterval(function () {
         if (PortWS.state == 0) {
         if (window.console) window.console.log('Info: begin reconnect.');
         PortWS.initialize();
         } else {
         if (window.console) window.console.log('Info: reconnect ok.');
         clearInterval(PortWS.reconnect);
         }
         }, 5000)
         }else{
         if (window.console) window.console.log('Info: reconnect exists.');
         }*/
    };

    ChatWS.socket.onerror = function (event) {
        if (window.console) window.console.log('Info: Port WebSocket connection error.' + event);
    };

    ChatWS.socket.onmessage = function (message) {
        //if (window.console) window.console.log(message.data);
        ChatWS.onmessage(message.data);
    };
};

ChatWS.initialize = function () {
    if (window.location.protocol == 'http:') {
        //ChatWS.connect('ws://localhost:8080/spring-websocket/chat');
        ChatWS.connect('ws://116.196.86.172:8080/spring-websocket/chat');
    } else {
        ChatWS.connect('wss://ww4.kanyanbao.com/newsadapter2/spring-websocket/chat');
    }
};

ChatWS.onmessage=function(m){
    try{
        //WsMessage 处理方法（自行定义在引用的地方）
        WsOnMessageProcess(m);
    }catch (e){
        if(window.console)
            console.log("WsOnMessageProcess方法不存在");
    }

};

ChatWS.onopen=function(m){
    if (window.console) window.console.log('ChatWS open.' + m);
    try{
        WsOnOpenProcess(m);
    }catch(e) {
        if(window.console)
            console.log("WsOnOpenProcess方法不存在");
    }
}

ChatWS.send_message=function(m){
    if(ChatWS.state==1){
        ChatWS.socket.send(m);
        if (window.console) window.console.log('socket open, message sent.' + m);
    }else{
        if (window.console) window.console.log('socket not open, message not send.' + m);
    }
}
$( window ).unload(function() {
    ChatWS.close();
});

$(document).ready(function() {
    if(ChatWS.check()) {
        if(ChatWS.initialize())
            if (window.console) window.console.log('ChatWS initialized.');
    }else{
        if (window.console) window.console.log('websocket not supported in browser.');
    }
});
